package com.zhuss.product.processor;

import java.util.*;

import com.zhuss.enums.StartStopEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.product.preline.CreatePrelineBo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.product.db.dao.ext.PrelineDao;
import com.zhuss.product.db.entity.Preline;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.DzcxException;
import com.zhuss.util.JsonUtil;


/**
 * @description: 招募创建
 * @author: mkh
 * @create: Mon Jan 04 13:38:02 CST 2021
 **/
@Component("prelineCreate")
public class PrelineCreate extends AbstractProcessor<Object, CreatePrelineBo> implements Processor<Object, CreatePrelineBo> {

    @Autowired
    private PrelineDao prelineDao;

    @Autowired
	@Qualifier("prelineRead")
	private PrelineRead prelineRead;


    /**
     * 校验
     */
    @Override
    public void check(CreatePrelineBo bo) throws DzcxException {
    	// TODO 创建 招募 校验， 请补充
    	String name = bo.getName();
    	if (name == null || "".equals(name.trim())) {
    		// throw new DzcxException(ExceptionCode.招募name为空);
    	}

    	List<Preline> list = prelineRead.list(name);
    	if (!list.isEmpty()) {
    		// throw new DzcxException(ExceptionCode.招募name已存在);
    	}
    }

    /**
     * 操作方法
     */
    @Override
    public Object process(CreatePrelineBo param) {
    	Preline record = new Preline();
    	record.setName(param.getName());
    	record.setPrelineNo(param.getPrelineNo());
    	record.setStartPointName(param.getStartPointName());
    	record.setEndPointName(param.getEndPointName());
    	record.setFirstTime(param.getFirstTime());
    	record.setRideType(param.getRideType());
    	record.setStatus(param.getStatus() == null ? StartStopEnum.启用.key() : param.getStatus());
    	record.setPrice(param.getPrice());
    	record.setSort(param.getSort());


    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	prelineDao.create(record);
    	param.setTargetId(record.getPrelineId());
        return null;
    }

    /**
     * 操作日志
     */
	@Override
	public Object log(CreatePrelineBo param) throws DzcxException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.商品.key());
    	record.setAction(LogActionEnum.招募新线创建.key());
    	record.setTargetId(param.getTargetId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());

    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
